package common.ui
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import kernel.core.Core;
	
	
	public class PageSpliter
	{
		public var container:Vector.<Object>;
		public var totalPage:uint;
		public var currentPage:uint;
		private var column:uint;
		private var row:uint;
		private var view:Sprite;
		private var pageList:Vector.<MovieClip>;
		private var btnPrev:SimpleButton;
		private var btnNext:SimpleButton;
		private var pview:Sprite;
		private var startPage:uint;
		private var type:uint;
		private var pageFun:Function;
		private var _boxClassName:String;
		private var _itemViewFunction:Function;
		private var _xyFunction:Function;
		private var _onePageCount:uint;
		
		public function PageSpliter(itemViewFunction:Function,onePageCount:uint=8,xyFunction:Function=null,boxClassName:String = 'newItemPageAsset')
		{
			_boxClassName = boxClassName;
			_itemViewFunction = itemViewFunction;
			if(xyFunction == null)
			{
				_xyFunction = defaultXYFunction;
			}else
			{
				_xyFunction = xyFunction;
			}
			_onePageCount = onePageCount;
			currentPage = 1;
			column = 3;
			row = 4;
			type=0;
			container = new Vector.<Object>();
		}
		public function initData(dataList:Array,total:uint,fun:Function):void
		{
			clear();
			pageFun = fun;
			totalPage = total/(column*row);
			if(total%(column*row) > 0)
			{
				totalPage += 1;
			}
		}
		private function defaultXYFunction():void
		{
			
		}
		private function clear():void
		{
			var len:uint = container.length;
			clearView(pview);
		}
		public function destroy():void
		{
			clear();
			if(view != null)
			{
				btnPrev.removeEventListener(MouseEvent.CLICK,onPrev);
				btnNext.removeEventListener(MouseEvent.CLICK,onNext);
				var len:uint = pageList.length;
				for(var i:uint=0;i<len;i++)
				{
					var mc:MovieClip=pageList.pop();
					mc.removeEventListener(MouseEvent.CLICK,onPageIndex);
				}
			}
			pageFun = null;
		}
		public function show(dis:Sprite):void
		{
			pview = dis;
			getPageIndex();
		}
		public function setItemPage(dis:Sprite):void
		{
			clearView(dis);
			if(view == null)
			{
				view = Core.creatSprite(_boxClassName);
				btnPrev = view.getChildByName("prevPage_btn") as SimpleButton;
				btnPrev.addEventListener(MouseEvent.CLICK,onPrev,false,0,true);
				btnNext = view.getChildByName("nextPage_btn") as SimpleButton;
				btnNext.addEventListener(MouseEvent.CLICK,onNext,false,0,true);
				pageList = new Vector.<MovieClip>();
				var mc:Sprite;
				var index:uint = 1;
				while (true) {
					mc = view.getChildByName('page_'+index+'sp') as MovieClip;
					if (mc == null) {
						break;
					} else {
						mc.addEventListener(MouseEvent.CLICK,onPageIndex,false,0,true);
						mc.buttonMode=true;
						mc.mouseChildren=false;
						pageList.push(mc);
						index++;
					}
				}
			}
			dis.addChild(view);
		}
		private function onPrev(evt:MouseEvent):void
		{
			if(currentPage > 1)
			{
				currentPage--;
				type=1;
				pageFun(currentPage);
			}
		}
		private function onNext(evt:MouseEvent):void
		{
			if(currentPage < totalPage)
			{
				currentPage++;
				type=0;
				pageFun(currentPage);
			}
		}
		private function onPageIndex(evt:MouseEvent):void
		{
			var mc:MovieClip=evt.currentTarget as MovieClip;
			var txt:TextField = mc.getChildByName('page_txt') as TextField;
			currentPage = uint(txt.text);
			pageFun(currentPage);
		}
		private function getPageIndex():void
		{
			clearView(pview);
			var len:uint = container.length;
			var rows:uint=0;
			for(var i:uint=0;i<len;i++)
			{
				if(i%column == 0&&i>0)
				{
					rows++;
				}
			}
			//分页
			var j:uint=0;
			var mc:MovieClip;
			var mcpage:MovieClip;
			var txt:TextField;
			len=pageList.length;
			if(len==0)return;
			if(totalPage <= len)
			{
				for(j=0;j<totalPage;j++)
				{
					mc=pageList[j];
					mc.buttonMode=true;
					txt = mc.getChildByName('page_txt') as TextField;
					txt.text=(j+1).toString();
					mcpage = mc.getChildByName('ItemPage') as MovieClip;
					mcpage.visible=currentPage == uint(txt.text);
				}
				for(j=totalPage;j<len;j++)
				{
					mc=pageList[j];
					mc.buttonMode=false;
					txt = mc.getChildByName('page_txt') as TextField;
					txt.text="";
					mcpage = mc.getChildByName('ItemPage') as MovieClip;
					mcpage.visible=false;
				}
			}else{
				if(type == 0)
				{
					mc=pageList[len-3];
					mcpage = mc.getChildByName('ItemPage') as MovieClip;
					if(mcpage.visible)
					{
						isMcpage=mcpage.visible;
						txt = mc.getChildByName('page_txt') as TextField;
						startPage = uint(txt.text);
					}
				}else{
					mc=pageList[2];
					mcpage = mc.getChildByName('ItemPage') as MovieClip;
					var isMcpage:Boolean=false;
					if(mcpage.visible)
					{
						isMcpage=mcpage.visible;
						txt = mc.getChildByName('txtPage') as TextField;
						startPage = uint(txt.text)-len+3;
					}
				}
				var total:uint=totalPage-len+2;
				for(j=0;j<len;j++)
				{
					mc=pageList[j];
					mc.buttonMode=true;
					mcpage = mc.getChildByName('ItemPage') as MovieClip;
					txt = mc.getChildByName('page_txt') as TextField;
					if(j==0)
					{
						txt.text="1";
					}else if(j==len-1)
					{
						txt.text=totalPage.toString();
					}else if(currentPage<5)
					{
						if(j==len-2)
						{
							txt.text="...";
							mc.buttonMode=false;
						}else{
							txt.text=(j+1).toString();
						}
					}else if(currentPage>total)
					{
						if(j==1)
						{
							txt.text="...";
							mc.buttonMode=false;
						}else{
							txt.text=(total+j-1).toString();
						}
					}else{
						if(j==1||j==len-2)
						{
							txt.text="...";
							mc.buttonMode=false;
						}else{
							if(isMcpage)
							{
								txt.text=(startPage+j-1).toString();
							}
						}
					}
					mcpage = mc.getChildByName('ItemPage') as MovieClip;
					mcpage.visible=currentPage == uint(txt.text);
				}
			}
		}
		private function clearView(dis:Sprite):void
		{
			if(dis==null)return;
			var index:uint = dis.numChildren;
			while (index != 0) {
				dis.removeChildAt(index-1);
				index--;
			}
		}
	}
}